﻿using BLL;
using models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace yuejilalala
{
    public partial class home : System.Web.UI.Page
    {
        ProductCountMarager marager = new ProductCountMarager();
        ProductTypeMarager ProductType = new ProductTypeMarager();
        ProductMarager Product = new ProductMarager();
        StoreMarager store = new StoreMarager();
        CartMarager cartMarager = new CartMarager();
        public static string dizhi = "";
        protected void Page_Load(object sender, EventArgs e)
        {


            Session["dizhi"] = "images/";
            dizhi = Session["dizhi"].ToString();
            if (!IsPostBack)
            {
                userDl();
                Rep_diyi.DataSource = marager.SumProductCount();
                Rep_diyi.DataBind();
                Repeater3.DataSource = Product.GetProductCount(Label1.Text);
                Repeater3.DataBind();

                Repeater4.DataSource = Product.GetProductCount(Label2.Text);
                Repeater4.DataBind();

                //店铺
                Repeater5.DataSource = store.StoreCount();
                Repeater5.DataBind();

                Repeater6.DataSource = Product.GetProductCounts(Label2.Text);
                Repeater6.DataBind();

                Repeater7.DataSource = store.SumStore();
                Repeater7.DataBind();

                //店铺
                Repeater8.DataSource = Product.GetProductCounts(Label1.Text);
                Repeater8.DataBind();

                Repeater9.DataSource = Product.GetProductCounts(Label3.Text);
                Repeater9.DataBind();

                Repeater10.DataSource = store.SumStore(); ;
                Repeater10.DataBind();

                Repeater11.DataSource = Product.GetProductCounts(Label3.Text);
                Repeater11.DataBind();
                if (Session["sessid"]!=null)
                {

                    J_MiniCartNum.Text = cartMarager.CartCount((Session["sessid"].ToString())).ProPrice.ToString();//显示出购物车内物品.数量
                }
            }

        }
        //最外层迭代的ItemCommand
        protected void Rep_diyi_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Repeater rep = e.Item.FindControl("Rept_diyi_er") as Repeater;//找到对象Rept_diyi_er
            rep.ItemCommand += new RepeaterCommandEventHandler(Rept_diyi_er_ItemCommand);


            if (e.CommandName == "leixing")
            {
                Response.Write("<script>alert('" + e.CommandArgument + "')</script>");
            }

        }
        //最外层迭代的ItemDataBound事件
        protected void Rep_diyi_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater rep = e.Item.FindControl("Rept_diyi_er") as Repeater;//找到对象Rept_diyi_er

                ProductCount productCount = e.Item.DataItem as ProductCount;//接收数据项
                string ProCount = Convert.ToString(productCount.Id);//获取填充的id Convert为固定写法


                rep.ItemDataBound += new RepeaterItemEventHandler(Rept_diyi_er_ItemDataBound);
                

                rep.DataSource = ProductType.GetProductCount(int.Parse(ProCount));//给里面的迭代绑定数据
                rep.DataBind();
              

            }
        }
        //最外层迭代的Rep_diyi_ItemCreated
        protected void Rep_diyi_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            Repeater rep =(e.Item.FindControl("Rept_diyi_er") as Repeater);//找到对象Rept_diyi_er
            //rep2 = e.Item.FindControl("Rept_diyi_er_yi") as Repeater;//找到对象Rept_diyi_er
            rep.ItemCommand += new RepeaterCommandEventHandler(Rept_diyi_er_ItemCommand);
            //rep2.ItemCommand += new RepeaterCommandEventHandler(Rept_diyi_er_yi_ItemCommand);

        }

        //Rept_diyi_er
        //第二次迭代的ItemDataBound
        void Rept_diyi_er_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

         
            Repeater rep2 = e.Item.FindControl("Rept_diyi_er_yi") as Repeater;//找到对象Rept_diyi_er_yi
            Session["Rept_diyi_er"] = rep2;
            ProductType productType = e.Item.DataItem as ProductType;//接收数据项
                                                                     //string ProType = Convert.ToString(productType.Id);//获取填充的id Convert为固定写法 
                                                                     // Product product = e.Item.DataItem as Product;//接收数据项
           
          
            rep2.DataSource = Product.SelProducByProType(productType.Id.ToString());//给第三层的迭代绑定数据
            rep2.DataBind();

            



        }
       
        //第三层迭代的ItemCommand
        void Rept_diyi_er_yi_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "proname")
            {
                string name = e.CommandArgument.ToString();
                Response.Redirect("search.aspx?name=" + name);
            }


        }
        //第二层迭代的ItemCommand事件
        void Rept_diyi_er_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
         
            if (e.CommandName=="pro")
            {
                string id = e.CommandArgument.ToString();
                Response.Redirect("search.aspx?id=" + id);
            }
          

        }



        protected void Button1_Click(object sender, EventArgs e)
        {
            string name = TextBox1.Text;
            Response.Redirect("search.aspx?name=" + name);
        }

       

      
        protected void Repeater7_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            Response.Redirect("search.aspx?typeid=" + id);//商品类型
        }

      

        protected void Repeater5_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            Response.Redirect("search.aspx?typeid=" + id);
        }

        protected void Repeater10_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            Response.Redirect("search.aspx?typeid=" + id);
        }

        protected void Repeater8_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            Response.Redirect("introduction.aspx?proid=" + id);
        }

        protected void Repeater6_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id= e.CommandArgument.ToString();
            Response.Redirect("introduction.aspx?proid=" + id);
        }

        protected void Repeater11_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();
            Response.Redirect("introduction.aspx?proid=" + id);
        }
        public void userDl()
        {
            if (Session["sess"] != null)
            {
                //用户存在则显示出来 并且隐藏注册和登陆显示
                User user = Session["sess"] as User;
                hl_dlm.Text ="用户名：" +user.UName;
                hl_dlm.Visible = true;//显示登陆名控件
                hl_dl.Visible = false;//请登陆隐藏
                hl_zc.Visible = false;//注册隐藏
            }

        }

        protected void Repeater9_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string name = e.CommandArgument.ToString();
            Response.Redirect("search.aspx?name=" + name);
        }

        protected void Repeater4_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string name = e.CommandArgument.ToString();
            Response.Redirect("search.aspx?name=" + name);
        }

        protected void Repeater3_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string name = e.CommandArgument.ToString();
            Response.Redirect("search.aspx?name=" + name);
        }

       
    }
}
